package com.rentalHome.controller;

import com.rentalHome.domain.Rental;
import com.rentalHome.domain.User;
import com.rentalHome.domain.Room;
import com.rentalHome.repository.RentalRepository;
import com.rentalHome.repository.RoomRepository;
import com.rentalHome.repository.UserRepository;
import com.rentalHome.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;

/**
 * Created by LeeXyan on 2017/02/14.
 */
@RestController
@RequestMapping("/rental")
public class RentalController {
    @Autowired
    private RentalRepository rentalRepository;

    @Autowired
    private RoomRepository roomRepository;

    @Autowired
    private UserRepository userRepository;

    /**
     * 得到所有的房间
     * @param userId
     * @return
     */
    @GetMapping("/roomList")
    public ResponseEntity roomList(@RequestParam("userId")Long userId){
        User user=userRepository.findOne(userId);
        return ResponseEntity.ok(roomRepository.findByUser(user));
    };

    /**
     * 添加新租客
     * @param roomName
     * @param customerName
     * @param customerId
     * @param customerTel
     * @param startDate
     * @param hasNet
     * @param basePayment
     * @param deposit
     * @param otherInfo
     * @return
     */
    @PostMapping("/add")
    public ResponseEntity RentalAdd(@RequestParam("roomName") String roomName,
                                    @RequestParam("customerName") String customerName,
                                    @RequestParam("customerId") String customerId,
                                    @RequestParam("customerTel") String customerTel,
                                    @RequestParam("startDate") Date startDate,
                                    @RequestParam("hasNet") Boolean hasNet,
                                    @RequestParam("basePayment") Double basePayment,
                                    @RequestParam("deposit") Double deposit,
                                    @RequestParam("otherInfo") String otherInfo) throws ParseException {

        String type="yyyy-MM-dd";
        Rental rental=new Rental();
        Room room= roomRepository.findByName(roomName);
        User user=new User();
        user.setId(1L);
        room.setIsEmpty(false);

        Rental rl=rentalRepository.findByRoomAndCustomerNameAndUser(room,customerName,user);
        Rental rt=null;
        if(rl==null){
            rental.setRoom(room);
            rental.setCustomerName(customerName);
            rental.setCustomerId(customerId);
            rental.setCustomerTel(customerTel);
            rental.setCreateDate(startDate);
            rental.setIsValid(true);
            rental.setStartDate(startDate);
            rental.setNextPayDate(DateUtil.nextMonth(startDate,type));
            rental.setHasNet(hasNet);
            rental.setBasePayment(basePayment);
            rental.setDeposit(deposit);
            rental.setOtherInfo(otherInfo);
            rental.setUser(user);
            rt=rentalRepository.save(rental);
        }else {
            rl.setCustomerId(customerId);
            rl.setCustomerTel(customerTel);
            rl.setCreateDate(startDate);
            rl.setIsValid(true);
            rl.setStartDate(startDate);
            rl.setNextPayDate(DateUtil.nextMonth(startDate,type));
            rl.setHasNet(hasNet);
            rl.setBasePayment(basePayment);
            rl.setDeposit(deposit);
            rl.setOtherInfo(otherInfo);
            rt=rentalRepository.save(rl);
        }


        return ResponseEntity.ok(rt);
    }


    /**
     * 查询历史租客
     * @param userId
     * @return
     */
    @GetMapping("/historicalLease")
    public ResponseEntity historicalLease(@RequestParam("userId")Long userId){
        User user=userRepository.findOne(userId);
        /*rentalRepository.findHistoryRentalByUser(user)*/
        return ResponseEntity.ok(rentalRepository.findByUserId(userId));
    }

    /**
     * 账单结算
     * @param userId
     * @return
     */
    @GetMapping("/billing")
    public ResponseEntity billingList(@RequestParam("userId")Long userId){
        User user=userRepository.findOne(userId);
        return ResponseEntity.ok( rentalRepository.findCurrentRentalByUser(user));
    }

    /**
     * 详情
     * @param roomNum
     * @param customerName
     * @return
     */
    @GetMapping("/details")
    public ResponseEntity details(@RequestParam("roomNum")String roomNum,@RequestParam("customerName")String customerName){
        Room room=roomRepository.findByName(roomNum);
        return ResponseEntity.ok(rentalRepository.findByRoomAndCustomerName(room,customerName));
    }

    /**
     * 查找
     * @param customerName
     * @return
     */
    @GetMapping("/search")
    public ResponseEntity search(@RequestParam("customerName")String customerName){

        return ResponseEntity.ok(rentalRepository.findByCustomerName(customerName));
    }

    /**
     *  删除
     * @param customerName
     * @return
     */
    @GetMapping("/delete")
    public ResponseEntity delete(@RequestParam("customerName")String customerName){
        Rental rental=rentalRepository.findByCustomerName(customerName);
        rental.setIsValid(false);
        return ResponseEntity.ok(rentalRepository.save(rental));
    }

}
